package com.tangshuai.video.controller;

import com.tangshuai.video.constant.SystemConstant;
import com.tangshuai.video.po.VideoInfo;
import com.tangshuai.video.po.VideoView;
import com.tangshuai.video.service.VideoInfoService;
import com.tangshuai.video.service.VideoViewService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * @Author TANGSHUAI
 * @Date 2024-03-03 21:40
 **/
@Controller
@RequestMapping("/video")
public class VideoInfoController {

    private Logger logger = LoggerFactory.getLogger(IndexController.class);

    @Autowired
    private VideoInfoService videoInfoService;

    @Autowired
    private VideoViewService videoViewService;

    /**
     * 分页查询视频信息
     * @param request
     * @param videoInfo
     * @return
     */
    /*@RequestMapping("/selectAllVideoInfo")
    public String selectAllVideoInfo(HttpServletRequest request, VideoInfo videoInfo) {
        System.out.println(videoInfo);
        String ipAddress = request.getRemoteAddr();
        logger.info("访问客户端IP地址: {}" ,ipAddress);
        IPage<VideoInfo> videoInfoIPage = videoInfoService.selectAllVideoInfo(1, 20);
        request.setAttribute("videoInfoIPage", videoInfoIPage);
        return "index";
    }*/

    /**
     * 视频详情
     * @param request
     * @param id
     * @return
     */
    @RequestMapping("/detail")
    public String detail(HttpServletRequest request,Integer id){
        VideoInfo videoInfo = videoInfoService.selectByIdVideoInfo(id);
        VideoView videoView=new VideoView();
        videoView.setVideoInfoId(videoInfo.getId());
        List<VideoView> videoViews = videoViewService.selectByVideoInfoId(videoView);
        String ipAddress = request.getRemoteAddr();
        logger.info("访问客户端IP地址: {}" ,ipAddress);
        logger.info("视频详情: {}" ,videoInfo);
        request.setAttribute("videoInfo",videoInfo);
        request.setAttribute("videoViews",videoViews);
        return "detail";
    }

    /**
     * 播放视频
     * @param request
     * @param id
     * @return
     */
    @RequestMapping("/videoPlay")
    public String video(HttpServletRequest request,Integer id){
        VideoView videoView = videoViewService.selectById(id);
        String ipAddress = request.getRemoteAddr();
        logger.info("访问客户端IP地址: {}" ,ipAddress);
        logger.info("播放视频: {}" ,videoView);
        VideoView view=new VideoView();
        view.setVideoInfoId(videoView.getVideoInfoId());
        List<VideoView> videoViewList = videoViewService.selectByVideoInfoId(view);
        request.setAttribute("videoViewList",videoViewList);
        request.setAttribute("videoView",videoView);
        return "video";
    }

    /**
     * 同步视频
     * @param directory
     * @return
     */
    @RequestMapping("/videoSync/{directory}")
    @ResponseBody
    @Transactional
    public String videoSync(@PathVariable("directory") String directory){
        if(null==directory){
            logger.error("请输入正确的目录：{}",directory);
            throw new NullPointerException("请输入正确的目录！");
        }
        logger.info("要同步的目录：{}",directory);
        // 查询数据库中是否存在该目录
        VideoInfo vf=videoInfoService.selectByVideoName(directory);
        if(null!=vf){
            logger.error("视频已存在，同步失败！{}",directory);
            throw new NullPointerException("视频已存在，同步失败！");
        }
        File fileDirectory=new File(SystemConstant.FILEPARTITION+directory);
        File[] files = fileDirectory.listFiles();
        VideoInfo videoInfo=new VideoInfo();
        videoInfo.setVideoName(directory);
        videoInfo.setArea("中国");
        videoInfo.setCreate_time(new Date());
        videoInfo.setStoryLine(directory);
        videoInfo.setVideoType(directory);
        videoInfo.setVideoClassification(1);
        videoInfo.setVideoImage("/tv/"+"load.gif");
        videoInfo.setVideoDescription(directory);
        videoInfo.setYears("2024");
        videoInfo.setUpdateInfo("最近更新");
        videoInfo.setWhichEpisode(0);
        videoInfoService.addVideoInfo(videoInfo);
        int index=1;
        String videoImage="";
        for (File file : files) {
            if(file.isFile()){
                // 不是图片则插入数据库
                if(!"jpg".equals(file.getName().substring(file.getName().lastIndexOf(".")+1))){
                    VideoView videoView=new VideoView();
                    videoView.setVideoInfoId(videoInfo.getId());
                    videoView.setVideoViewIndex("第"+index+"集");
                    videoView.setUploadTime(new Date());
                    videoView.setVideoUrl("/tv/"+directory+"/"+file.getName());
                    videoViewService.addVideoView(videoView);
                    index+=1;
                }else{
                    videoImage=file.getName();
                }
            }
        }
        logger.info("视频同步成功!{}",directory);
        if(!StringUtils.isEmpty(videoImage)){
            logger.info("视频封面URL:{}",videoImage);
            videoInfo.setVideoImage("/tv/"+directory+"/"+videoImage);
        }
        videoInfo.setWhichEpisode(index);
        videoInfoService.updateVideoInfo(videoInfo);
        return "同步成功";
    }

}
